<#include "/WEB-INF/views/admin/common/layout/__mainlayout.html">
<#include "/WEB-INF/views/admin/common/__pagination.html"> <#assign
navLink='admin:index' /> <#assign pageCss>
<link rel="stylesheet"
	href="${ctx}/resource/validationengine/css/validationEngine.jquery.css">
</#assign> <#assign pageScript>
<script type="text/javascript"
	src="${ctx}/resource/validationengine/js/jquery.validationEngine-zh_CN.js"></script>
<script type="text/javascript"
	src="${ctx}/resource/validationengine/js/jquery.validationEngine.js"></script>
<script type="text/javascript">

$(document).ready(function() {
	
	$("#exportlog").click(function(){
		var searchDate = $("#search_date").val();
		if(searchDate == ''){
			layer.msg('Please choose the date', {
				icon : 2,
				time : 1000,
			});
			return;
		}
		var dates = searchDate.split(' - ');
		var begin  = new Date(dates[0].replace(/-/g, "/"));
		var end = new Date(dates[1].replace(/-/g, "/"));
		var days = end.getTime() - begin.getTime();
		var time = parseInt(days / (1000 * 60 * 60 * 24));
		if(time>90){
			layer.msg('maximum 90 days data can be export', {
				icon : 2,
				time : 1000,
			});
			return;
		}
		
		
		var form = $("#searchForm");
		form.attr("action","${ctx}/admin/export");
		form.submit();
		form.attr("action","${ctx}/admin/index")
		console.log(form.attr("action"));
	});
	
	
	laydate.render({
	    elem: '#search_date'
	    ,range: true
	  });
	$('#search_agentname').chosen({width:"100%"}).change(function(){
		var agentId = $("#search_agentname").val();
		$.ajax({
            type: "GET",
            url: "${ctx}/admin/onChange?id="+agentId,
            dataType: "json",
            success: function(data){
            	var options = "<option value='-1'>choose a user</option>";
            	for(var i =0;i<data.length;i++){
            		var v = data[i];
            		options+="<option value='"+v.id+"'>"+v.name+"</option>";
            	}
            	$('#search_username').html(options);
        		$('#search_username').trigger("chosen:updated");
             }
        });
		
	});
	$('#search_username').chosen({width:"100%"});
	
	
	$("#batch_pay").click(function(){
		var selectedItem = [];
		$(".checkbox:checked").each(function(){
			selectedItem.push($(this).val());
		});
		var selected = selectedItem.join(",");
		if(selected != ''){
			$.ajax({
	            type: "GET",
	            url: "${ctx}/admin/sys/settlement/batchCheck?id="+selected,
	            dataType: "json",
	            success: function(data){
	            		if(data.success){
	            			layer.msg(data.errMsg, {
	            				icon : 1,
	            				time : 1000,
	            				end : function() {
	            					window.location.reload();
	            				}
	            			});
	            		}else{
	            			layer.msg(data.errMsg, {
	            				icon : 2,
	            				time : 1000,
	            				end : function() {
	            					window.location.reload();
	            				}
	            			});
	            		}
	                 }
	        });
		}else{
			layer.msg("please choose data", {
				icon : 2,
				time : 1000,
				end : function() {
					window.location.reload();
				}
			});
		}
		
	});
});

    function update(id) {
        layer.open({
            title: 'do settlement',
            type: 2,
            area: ['60%', '40%','no'],
            scrollbar: false,
            content: '${ctx}/admin/sys/settlement/update?id=' + id,
            end: function () {
                window.location.reload();
            }
        });
    	
    }

    function selectAll(checked) {
        $('.checkbox').prop('checked',checked);
    }

    function changeSelectAll(){
        var items = $('.checkbox');
        var allSelect = $('#AllSelectChk');
        var flag = 1;
        items.each(function (e,ele) {
            if(!$(ele).prop('checked')){
                flag = 0;
            }
        })
        allSelect.prop('checked',!!flag);
    }

</script>
</#assign> <@mainlayout>
<div class="row-content  am-cf ">
	<div class="form-top" data-am-sticky="{animation: 'slide-top'}">
		<form id="searchForm" class="am-form-inline" action="${ctx}/admin/index" method="post">
		<#if currentAgent??>
			<div class="am-u-sm-12 am-u-md-3">
				<div style="width:90%"
					class="am-input-group am-input-group-sm tpl-form-border-form cl-p">
					<select disabled="disabled" name="search_agentname" id="search_agentname">
						<option value="${currentAgent.id}">${currentAgent.name}</option>
					</select>
				</div>
			</div>
		<#else>
			<div class="am-u-sm-12 am-u-md-3">
				<div style="width:90%"
					class="am-input-group am-input-group-sm tpl-form-border-form cl-p">
					<select  name="search_agentname" id="search_agentname">
						<option value="0">choose a agent</option>
						<#list agents as agent>
							<#if search_agentname == agent.id>
								<option value="${agent.id}" selected="selected">${agent.name}</option>
							<#else>
								<option value="${agent.id}" >${agent.name}</option>
							</#if>
						</#list>
						
					</select>
				</div>
			</div>
		</#if>
			<div class="am-u-sm-12 am-u-md-3">
				<div style="width:90%"
					class="am-input-group am-input-group-sm tpl-form-border-form cl-p">
					<select name="search_username" id="search_username">
						<option value="-1">choose a user</option>
						<#list customers as user>
							<#if search_username == user.id>
								<option value="${user.id}" selected="selected">${user.name}</option>
							<#else>
								<option value="${user.id}" >${user.name}</option>
							</#if>
						</#list>
					</select>
				</div>
			</div>
			
			<div class="am-u-sm-12 am-u-md-2">
				<div style="width:90%"
					class="am-input-group am-input-group-sm tpl-form-border-form cl-p">
					<input type="text" class="am-form-field " name="search_offerid"
									value="${search_offerid}" placeholder="offerId">
				</div>
			</div>
			
			<div class="am-u-sm-12 am-u-md-3">
				<div style="width:100%"
					class="am-input-group am-input-group-sm tpl-form-border-form cl-p">
					<input type="text" class="" readonly 
						name="search_date" value="${search_date}" id="search_date"
						placeholder="day"> <span class="am-input-group-btn">
						<button
							class="am-btn  am-btn-default am-btn-success tpl-table-list-field am-icon-search"
							type="submit"></button>
					</span>
				</div>
			</div>
		</form>
	</div>
</div>
<div class="row-content am-cf">
	<div class="row  am-cf">
		<div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
			<div class="widget widget-primary am-cf">
				<div class="widget-statistic-header">click</div>
				<div class="widget-statistic-body">
					<div class="widget-statistic-value">${totalClick!0}</div>
					<span class="widget-statistic-icon am-icon-credit-card-alt"></span>
				</div>
			</div>

		</div>
		<div class="am-u-sm-12 am-u-md-6 am-u-lg-3" style="float:left">
			<div class="widget widget-primary am-cf">
				<div class="widget-statistic-header">revenue</div>
				<div class="widget-statistic-body">
					<div class="widget-statistic-value">${totalRevenue!0.0}</div>
					<span class="widget-statistic-icon am-icon-credit-card-alt"></span>
				</div>
			</div>
		</div>
		<#if !currentAgent??>
		<div class="am-u-sm-12 am-u-md-6 am-u-lg-3">
			<div class="widget widget-purple am-cf">
				<div class="widget-statistic-header">deduct click</div>
				<div class="widget-statistic-body">
					<div class="widget-statistic-value">${deductClick!0}</div>
					<span class="widget-statistic-icon am-icon-support"></span>
				</div>
			</div>
		</div>
		<div class="am-u-sm-12 am-u-md-6 am-u-lg-3">
			<div class="widget widget-purple am-cf">
				<div class="widget-statistic-header">deduct revenue</div>
				<div class="widget-statistic-body">
					<div class="widget-statistic-value">${deductRevenue!0.0}</div>
					<span class="widget-statistic-icon am-icon-support"></span>
				</div>
			</div>
		</div>
		</#if>
	</div>
</div>
<div class="widget-body  widget-body-lg am-fr">
					<form action="${ctx}/admin/export" method="post">
						<div class="am-u-sm-12 am-u-md-12 am-u-lg-1 am-fr">
							<div class="am-input-group am-input-group-sm tpl-form-border-form cl-p">
									<span class="am-input-group-btn">
									<button id="exportlog"
										class="am-btn  am-btn-default am-btn-success tpl-table-list-field"
										type="button">
										<span class="am-icon-paypal">export</span> 
									</button>
								</span>
							</div>
						</div>
					</form>
				<div class="am-u-sm-12 am-scrollable-horizontal">
                  <table width="100%" class="am-table am-table-compact tpl-table-black am-text-nowrap am-table-hover" id="example-r">
                      <thead>
                          <tr>
                              <th >statsDay</th>
                              <#if !currentAgent??>
                              <th >agentId</th>
                              <th >agentName</th>
                              </#if>
                              <#if !currentAgent??>
                              <th >userId</th>
                              </#if>
                              <th >userName</th>
                              <#if !currentAgent??>
                              <th >deviceId</th>
                              </#if>
                              <th >offerId</th>
                              <#if !currentAgent??>
                              <th >price</th>
                              </#if>
                             <#if !currentAgent??>
                              <th >deduct_click</th>
                             </#if>
                              <th >total_click</th>
                          </tr>
                      </thead>
                      <tbody>
                      	<#list pageList.list as stats>
                          <tr class="gradeX">
                              <td >${stats.statsDay!}</td>
                              <#if !currentAgent??>
                              <td >${stats.agentId!}</td>
                              <td >${stats.agentName!}</td>
                              </#if>
                              <#if !currentAgent??>
                              <td >${stats.userId!}</td>
                              </#if>
                              <td >${stats.userName!}</td>
                              <#if !currentAgent??>
                              <td >${stats.deviceId!}</td>
                              </#if>
                              <td >${stats.offerId!}</td>
                              <#if !currentAgent??>
                              <td >${stats.price!}</td>
                              </#if>
                            <#if !currentAgent??>
                              <td >${stats.buckle!}</td>
                            </#if>
                            <#if currentAgent??>
                              <td >${stats.successClick!0}</td>
                            <#else>
                            	<td >${stats.successClick + stats.buckle}</td>
                            </#if>
                          </tr>
                          </#list>
                          <!-- more data -->
                      </tbody>
                  </table>
              </div>
              <@pagination page=pageList url='/admin/index?search_agentname=${search_agentname}&search_username=${search_username}&search_offerid=${search_offerid}&search_date=${search_date}' />
              </div>
</@mainlayout>

